﻿using System.Collections.Generic;
using System.Web.UI;
using JQuery.WebKit.Base;
using JQuery.WebKit.WebCommon;
using JQuery.WebKit.WebCommon.Attributes;

#region resources
[assembly: WebResource("JQuery.WebKit.JValidation.jquery.validationEngine-en.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JValidation.jquery.validationEngine-en.min.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JValidation.jquery.validationEngine.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JValidation.jquery.validationEngine.min.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JValidation.validationEngine.jquery.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("JQuery.WebKit.JValidation.template.css", "text/css", PerformSubstitution = true)]
#endregion

namespace JQuery.WebKit
{
    public static class JValidation
    {
        public static void AddValidation(this JControl control, ValidationFlags validationFlags)
        {
            AddValidation(control, validationFlags, 0, 0);
        }

        public static void AddValidation(this JControl control, ValidationFlags validationFlags, int length, int precision)
        {
            List<string> validationList = new List<string>();

            validationFlags = control.ValidationFlags | validationFlags;

            if (length == 0 && control.Length > 0)
            {
                length = control.Length;
            }
            if (precision == 0 && control.Precision > 0)
            {
                precision = control.Precision;
            }

            if ((validationFlags & ValidationFlags.Email) != 0)
            {
                validationList.Add("email");
            }

            if ((validationFlags & ValidationFlags.Integral) != 0)
            {
                //validationList.Add("onlyNumber");
            }

            if ((validationFlags & ValidationFlags.Mandatory) != 0)
            {
                validationList.Add("required");
            }

            if ((validationFlags & ValidationFlags.Negative) != 0)
            {

            }

            if ((validationFlags & ValidationFlags.NonNegative) != 0)
            {

            }

            if ((validationFlags & ValidationFlags.Numeric) != 0)
            {
                validationList.Add("onlyNumber");
            }

            if ((validationFlags & ValidationFlags.NumericNotIntegral) != 0)
            {

            }

            if ((validationFlags & ValidationFlags.Positive) != 0)
            {

            }

            if ((validationFlags & ValidationFlags.Text) != 0)
            {

            }

            if (length > 0)
            {
                validationList.Add(string.Format("length[0,{0}]", length));
            }
            if (validationList.Count > 0)
            {
                string css = control.Attributes["class"] ?? string.Empty;
                if (css.Contains("validate"))
                {
                    css = css.RemoveCssClass("validate*");
                }
                css = css.AddCssClass(string.Format("validate[{0}]", string.Join(",", validationList)), true);
                control.Attributes["class"] = css;
            }
            control.ValidationFlags = validationFlags;
            control.Length = length;
            control.Precision = precision;
        }

        public static void RegisterValidation(this WebControlBase parent)
        {
            parent.RegisterValidation(null);
        }

        public static void RegisterValidation(this WebControlBase parent, JValidationOptions options)
        {
            parent.RegisterCSSResource("JQuery.WebKit.JValidation.validationEngine.jquery.css");
            parent.RegisterCSSResource("JQuery.WebKit.JValidation.template.css");
            parent.RegisterJQueryResource("JQuery.WebKit.JValidation.jquery.validationEngine-en.js");
            parent.RegisterJQueryResource("JQuery.WebKit.JValidation.jquery.validationEngine.js");
            string script = string.Format("$('#{0}').validationEngine();", parent.ClientID);
            if (options != null)
            {
                script = string.Format("$('#{0}').validationEngine({1});", parent.ClientID, options.GetJSProperties("options"));
            }
            //parent.RegisterJQueryResource(script);
            parent.AddBodyScript(script);
            //parent.Page.Resources.Add(new WebScriptResource(typeof(WebControlBase), ResourceType.Javascript, script, true, LoadEvent.DocumentReady));
        }


        //public string TargetControlID { get; set; }

        //public JValidation()
        //{

        //}

        //protected override void OnPreRender(EventArgs e)
        //{
        //    JControl ctrl = Page.FindControlRecursive(TargetControlID, IDType.Server) as JControl;
        //    if (ctrl == null)
        //    {
        //        return;
        //    }

        //    string css = ctrl.Attributes["class"];
        //    HtmlUtil.AddCssClass(css, string.Format(""), true);

        //    ctrl.Attributes["class"] = css;
        //    //base.OnPreRender(e);
        //}

        //protected override void Render(System.Web.UI.HtmlTextWriter writer)
        //{
        //    //do not render
        //}
    }
}
